# coding:utf-8

from qa.interface.app.parameters import ia_token, ia_http
import hashlib
import json, os
import time


def myloader(my_data, litUrl, post_data, url_ym, gest_uid):
    success = 0
    fail = 0
    faildata = []
    for data in my_data:
        username = data['username']
        passwd = hashlib.sha1(data['password']).hexdigest()
        uid = data['uid']
        try:
            if os.path.exists("auth.txt"):
                os.remove('auth.txt')
            ia_token.addHeader(1, username, passwd, url_ym, 1)  # 主态账号需要鉴权
            auth = eval(ia_token.get_auth(1))
            myhttp = ia_http.py_http()
            for myurl in litUrl:
                if gest_uid != '':
                    if int(gest_uid) == 1:
                        myurl = ''.join(myurl) % str(uid)
                    elif int(gest_uid) == 2:
                        myurl = ''.join(myurl) % (str(uid), str(uid))
                page = myhttp.http_read(myurl, auth, 5, post_data)
            success += 1
            os.remove('auth.txt')
        except:
            fail += 1
            faildata.append({username: data['password']})
            print "操作失败,失败账号为%s, 密码为%s" % (username, data['password'])
    if faildata == []:
        return u"Congratulations, successfully run%s, failed 0 times" % success,
    else:
        return u"Sorry, successfully ran%s,%s times failed to run, fail account %s" % (
        success, str(fail), str(faildata))
